package com.ssm.helong.shop.service.impl;

import com.ssm.helong.shop.Dao.UserDao;
import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.entity.User;
import com.ssm.helong.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public String register(String userLoginName, String userLoginPassword,String userPhone) {
        if (userDao.selectByLoginName(userLoginName) != null) {
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        Date date = new Date();
        DateFormat df = DateFormat.getDateTimeInstance();
        User registerUser = new User();
        registerUser.setUserLoginName(userLoginName);
        registerUser.setUserLoginPassword(userLoginPassword);
        registerUser.setUserNickName(userLoginName);
        registerUser.setUserPhone(userPhone);
        registerUser.setUserCreateTime(df.format(date));
        registerUser.setUserIsDeleted(0);
        if (userDao.insertSelective(registerUser) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String passwordLogin(String userLoginName, String userLoginPassword) {
        User user = userDao.selectByLoginNameAndPassword(userLoginName, userLoginPassword);
        if (user != null) {
            return userLoginName;
        }
        return ServiceResultEnum.LOGIN_ERROR.getResult();
    }

    @Override
    public String phoneLogin(String userPhone, String code) {
        User user = userDao.selectByPhone(userPhone);
        if (user != null) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.LOGIN_ERROR.getResult();
    }

}
